package com.icode.web.controller.house;

import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.dto.house.HouseWeekDTO;
import com.icode.core.model.house.HouseBaseArea;
import com.icode.core.model.house.HouseWeek;
import com.icode.core.shared.DataToJson;
import com.icode.service.house.HouseService;
import com.icode.service.house.HouseWeeklyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-24
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Controller("HouseWeekController")
@RequestMapping("/house_week")
public class HouseWeekController {

    @Autowired
    private HouseWeeklyService houseWeeklyService;
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String show(Model model) {
        List<HouseInfoDTO> houseInfoDTOs = houseWeeklyService.findAllHouseInfos();
        model.addAttribute("houseInfoDTOs", houseInfoDTOs);
        return "back/houseBase/houseWeekOverView";
    }

    //maybe it can be deleted
//    @RequestMapping(value = "update", method = RequestMethod.GET)
//    public String update(Model model) {
//        List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
//        model.addAttribute("areas", areas);
//        return "back/houseBase/houseWeek";
//    }
    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String update(Model model) {
        List<HouseInfoDTO> houseInfoDTOs = houseService.getHouseInfoDTOs();
        model.addAttribute("houseInfoDTOs", houseInfoDTOs);
        return "back/houseBase/houseWeek";
    }

    @RequestMapping(value = "modify/{houseWeekGuid}")
    public String modify(@PathVariable String houseWeekGuid, Model model) {
        HouseWeekDTO houseWeekDTO = houseWeeklyService.findHouseWeekByGuid(houseWeekGuid);
        model.addAttribute("houseWeekDTO", houseWeekDTO);
        String houseName = houseWeekDTO.getHouseInfoName();
        model.addAttribute("houseName", houseName);
        return "back/houseBase/houseWeekNew";
    }

    @RequestMapping(value = "updatebase", method = RequestMethod.POST)
    public String update(HouseWeekDTO houseWeekDTO) {
        houseWeeklyService.upDateBase(houseWeekDTO);
        return "redirect:/house_week/modify/" + houseWeekDTO.getGuid();
    }

    @RequestMapping(value = "updatehouseweektime", method = RequestMethod.POST)
    @ResponseBody
    public void updatehouseweektime(String start, String end, String houseweektimeguid, HttpServletResponse response) {
        houseWeeklyService.updatehouseweektime(start, end, houseweektimeguid);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }

    @RequestMapping(value = "divide_time", method = RequestMethod.POST)
    private void divideTime(String houseWeekGuid, String time, HttpServletResponse response) {
        houseWeeklyService.divideWeekTime(houseWeekGuid, time);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }

    @RequestMapping(value = "findHouseInfo/{area}", method = RequestMethod.GET)
    public void findHouseInfoByArea(@PathVariable String area, HttpServletResponse response) {
        houseWeeklyService.findHouseInfoByArea(area, response);
    }

    /**
     * save houseWeek
     *
     * @param area
     * @param guids
     * @param week
     * @param price
     * @param description
     * @return
     */
    @RequestMapping(value = "divide", method = RequestMethod.POST)
    @ResponseBody
    public String divideHouseWeek(String area, String guids, String week, String price, String description) {
        houseWeeklyService.divideHouseWeek(area, guids, week, price, description);
        return "success";
    }

    @RequestMapping(value = "addweek", method = RequestMethod.POST)
    @ResponseBody
    public String addweek(String houseInfoguid, String week, String price, String description, String totalNum) {
        houseWeeklyService.addweek(houseInfoguid, week, price, description, totalNum);
        return "success";
    }

    @RequestMapping(value = "find_house_week/{houseInfoGuid}", method = RequestMethod.GET)
    public void findHouseWeek(@PathVariable String houseInfoGuid, HttpServletResponse response) {
        List<HouseWeekDTO> houseWeekDTOs = houseWeeklyService.findHouseWeekByHouseInfoGuid(houseInfoGuid);
        houseWeeklyService.findHouseWeekByArea(houseWeekDTOs, response);
    }

    @RequestMapping(value = "findhouseweek/{houseInfoGuid}/{week}", method = RequestMethod.GET)
    public String findHouseWeekByWeekName(@PathVariable String houseInfoGuid, @PathVariable String week, Model model) {
        HouseWeekDTO houseWeekDTO = houseWeeklyService.findHouseWeekByHouseInfoAndweekName(houseInfoGuid, week);
        List<HouseInfoDTO> houseInfoDTOs = houseWeeklyService.findAllHouseInfos();
        model.addAttribute("houseInfoGuid", houseInfoGuid);
        model.addAttribute("week", week);
        model.addAttribute("houseInfoDTOs", houseInfoDTOs);
        model.addAttribute("houseWeek", houseWeekDTO);
        return "back/houseBase/houseWeek";
    }


    @RequestMapping(value = "del/{guids}", method = RequestMethod.GET)
    @ResponseBody
    public String deleteHouseWeek(@PathVariable String guids) {
        houseWeeklyService.deleteHouseWeekByGuids(guids);
        return "success";
    }

    @RequestMapping(value = "delhouseweektime/{guids}/{houseweekguid}", method = RequestMethod.GET)
    private String houseWeekTimeDel(@PathVariable String guids, @PathVariable String houseweekguid) {
        houseWeeklyService.deleteHouseWeekTimeByGuids(guids);
        return "redirect:/house_week/modify/" + houseweekguid;
    }


    @RequestMapping(value = "beforebatchaddhouseweektime", method = RequestMethod.GET)
    public String beforebatchaddhouseweektime(Model model) {
        List<HouseInfoDTO> houseInfoDTOs = houseService.getHouseInfoDTOsHaveWeek();
        model.addAttribute("houseInfoDTOs", houseInfoDTOs);
        return "back/houseBase/batchAddHouseWeekTime";
    }

    @RequestMapping(value = "batchaddhouseweektime", method = RequestMethod.POST)
    @ResponseBody
    public String batchaddhouseweektime(String guids, String weekH, String weekW, String weekP, String weekD) {
        houseWeeklyService.addweeks(guids, weekH, weekW, weekP, weekD);
        return "success";
    }

    @RequestMapping(value = "beforebatchupdatehouseweektime/{houseBaseArea}", method = RequestMethod.GET)
    public String beforebatchupdatehouseweektime(@PathVariable String houseBaseArea, Model model) {
        List<HouseInfoDTO> houseInfoDTOs = houseService.findhouseInfoByhouseBaseArea(houseBaseArea);
        String area = HouseBaseArea.valueOf(houseBaseArea).getLabel();
        model.addAttribute("houseInfoDTOs", houseInfoDTOs);
        model.addAttribute("area", area);
        return "back/houseBase/batchUpdateHouseWeekTime";
    }

    @RequestMapping(value = "findweektime/{houseInfoguid}/{weekName}", method = RequestMethod.GET)
    @ResponseBody
    public HouseWeekDTO findweektime(@PathVariable String houseInfoguid, @PathVariable String weekName) {
        HouseWeekDTO houseWeekDTO = houseWeeklyService.findHouseWeekByHouseInfoAndweekName(houseInfoguid, weekName );
        return houseWeekDTO;
    }

    @RequestMapping(value = "batchupdatehouseweektime/{houseInfoguids}/{weekName}/{weekTimes}", method = RequestMethod.GET)
    @ResponseBody
    public HashMap<String,Boolean> batchupdatehouseweektime(@PathVariable String houseInfoguids, @PathVariable String weekName, @PathVariable String weekTimes) {
        HashMap<String, Boolean> hashMap = new HashMap<>();
        try{
            houseWeeklyService.batchupdatehouseweektime(houseInfoguids, weekName, weekTimes);
            hashMap.put("success",true);
            return hashMap;
        }
        catch(Exception e){
            hashMap.put("success",false);
            return hashMap;
        }
    }
}